import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/**
 * 文件解析器类，用于解析包含歌曲信息的文件
 */
public class FileParser {
    private String songName;
    private String songAuthorName;
    private String beatsPerMinute;
    private String songFilename;

    /**
     * 获取歌曲名
     *
     * @return 歌曲名
     */
    public String getSongName() {
        return songName;
    }

    /**
     * 获取歌手名
     *
     * @return 歌手名
     */
    public String getSongAuthorName() {
        return songAuthorName;
    }

    /**
     * 获取BPM
     *
     * @return BPM
     */
    public String getBeatsPerMinute() {
        return beatsPerMinute;
    }

    /**
     * 获取歌曲文件名
     *
     * @return 歌曲文件名
     */
    public String getSongFilename() {
        return songFilename;
    }

    /**
     * 解析文件并获取歌曲信息
     *
     * @param filePath 文件路径
     */
    public void parseFile(String filePath) {
        try {
            File file = new File(filePath + "Info.dat");
            String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
            JSONObject jsonObject = JSON.parseObject(content);
            songName = jsonObject.getString("_songName");
            songAuthorName = jsonObject.getString("_songAuthorName");
            beatsPerMinute = jsonObject.getString("_beatsPerMinute");
            songFilename = jsonObject.getString("_songFilename");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}